// utils/util.js
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 格式化日期为 YYYY-MM-DD
const formatDate = (date = new Date()) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
}

// 计算时间差（分钟）
const getTimeDiff = (startTime, endTime) => {
  const diff = endTime - startTime
  return Math.floor(diff / (1000 * 60))
}

// 格式化时间为 HH:MM
const formatTimeHM = (date) => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  return `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`
}

// 深拷贝对象
const deepClone = (obj) => {
  return JSON.parse(JSON.stringify(obj))
}

// 生成唯一ID
const generateId = () => {
  return 'task_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
}

module.exports = {
  formatTime,
  formatDate,
  getTimeDiff,
  formatTimeHM,
  deepClone,
  generateId
}